package leeon.mobile.autoip;

import java.util.ArrayList;
import java.util.List;

import leeon.mobile.autoip.IPUtil.IpSetting;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Main extends ListActivity {
	
	private WifiManager m;
	private List<IpSetting> list;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		super.onCreate(savedInstanceState);
		registerForContextMenu(getListView());
		
		m = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
		
		IpSetting s = getCurrentIpSetting();
		list = getConfList();
		setListAdapter(new IpConfigListAdapter(list));
		
		String t = "wifi closed";
		if (s != null) {
			t = s.toString();
		} else if (m.isWifiEnabled()) {
			t = getDhcp();
		}
		
		if (customTitleSupported) {
			getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
			TextView title = (TextView)findViewById(R.id.ipTitle);
			title.setText(t);
		} else {
			this.setTitle(t);
		}
		
	}
	
	@Override 
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
		}
	}
	
	private static final int CONTEXT_MENU_REMOVE = 0;
	//private static final int CONTEXT_MENU_USE = 1;
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("操作配置");
		
		menu.add(0, CONTEXT_MENU_REMOVE, CONTEXT_MENU_REMOVE, "删除配置");
		//menu.add(0, CONTEXT_MENU_USE, CONTEXT_MENU_USE, "应用配置");
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		IpSetting setting = (IpSetting)getListAdapter().getItem(info.position);
		if (setting != null) {
			switch (item.getItemId()) {
				case CONTEXT_MENU_REMOVE:
					removeOne(setting.ssid);
					break;
				default:					
					break;
			}
		}
		return super.onContextItemSelected(item);
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, 1, 1, "SAVE IT");
		menu.add(0, 2, 2, "EDIT IT");
        return result;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			saveCurrent();
			break;
		case 2:
			editCurrent();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	
	private IpSetting getCurrentIpSetting() {
		if (m.isWifiEnabled()) {
			if (isStatic()) {
				return getStatic();
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	private boolean isStatic() {
		int i = Settings.System.getInt(
			getContentResolver(), Settings.System.WIFI_USE_STATIC_IP, 0);
		return i == 1;
	}
	
	private IpSetting getStatic() {
		String ssid = m.getConnectionInfo().getSSID();
		IpSetting s = new IPUtil().new IpSetting(
			ssid,
			Settings.System.getString(
				getContentResolver(), Settings.System.WIFI_STATIC_IP),
			Settings.System.getString(
				getContentResolver(), Settings.System.WIFI_STATIC_NETMASK),
			Settings.System.getString(
				getContentResolver(), Settings.System.WIFI_STATIC_GATEWAY),
			Settings.System.getString(
				getContentResolver(), Settings.System.WIFI_STATIC_DNS1),
			Settings.System.getString(
				getContentResolver(), Settings.System.WIFI_STATIC_DNS2)
		);
		return s;
	}
	
	private String getDhcp() {
		String ssid = m.getConnectionInfo().getSSID();
		return "SSID:" + ssid + "\n" + m.getDhcpInfo().toString();
	}
	
	private List<IpSetting> getConfList() {
		List<IpSetting> ret = new ArrayList<IpSetting>();
		String[] ssidList = IPUtil.getSsidList(this);
		if (ssidList == null) return ret;
		
		for (String ssid : ssidList) {
			IpSetting setting = IPUtil.getIpSettingBySsid(this, ssid);
			ret.add(setting);
		}
		return ret;
	}
	
	private void saveCurrent() {
		if (m.isWifiEnabled() && isStatic()) {
			IpSetting s = getStatic();
			IPUtil.saveOrUpdateIpSettingBySsid(this, s);
			((BaseAdapter)this.getListView().getAdapter()).notifyDataSetChanged();
		}
	}
	
	private void editCurrent() {
		startActivity(new Intent(Settings.ACTION_WIFI_IP_SETTINGS));
	}
	
	private void removeOne(String ssid) {
		IPUtil.removeIpSettingBySsid(this, ssid);
		((BaseAdapter)this.getListView().getAdapter()).notifyDataSetChanged();
	}
	
	
	private class IpConfigListAdapter extends BaseAdapter {

		private List<IpSetting> list;

		public IpConfigListAdapter(List<IpSetting> list) {
			this.list = list;
		}

		public int getCount() {
			return list.size();
		}

		public Object getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View tv;
			if (convertView == null) {
				tv = LayoutInflater.from(Main.this).inflate(android.R.layout.simple_list_item_1, parent, false);
			} else {
				tv = convertView;
			}
			
			TextView t1 = (TextView)tv.findViewById(android.R.id.text1);
			t1.setText(list.get(position).toString());
			
			return tv;
		}
	}
}